package com.cskaoyan.service.issue;

import com.cskaoyan.bean.MallCategoryExample;
import com.cskaoyan.bean.MallIssue;
import com.cskaoyan.bean.MallIssueExample;
import com.cskaoyan.mapper.MallIssueMapper;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.vo.BaseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: mjm
 * @Date: 2021/11/30
 * @Time: 22:07
 * @Description:
 */
@Service
public class IssueServiceImpl implements IssueService{
    @Autowired
    MallIssueMapper mallIssueMapper;


    @Override
    public BaseData issueList(PageBo param, String question) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        //List<User> users = userMapper.select(username, mobile, param.getSort(), param.getOrder());

        //逻辑删除的类目不显示
        MallIssueExample example = new MallIssueExample();
        example.setOrderByClause(param.getSort() + " " + param.getOrder());

        MallIssueExample.Criteria criteria = example.createCriteria();

        if(question != null && !"".equals(question)){
            criteria.andQuestionLike("%" + question + "%");
        }
        criteria.andDeletedEqualTo(false);


        List<MallIssue> mallIssues = mallIssueMapper.selectByExample(example);

        PageInfo info = new PageInfo(mallIssues);
        long total = info.getTotal();

        return BaseData.list(mallIssues,total);
    }

    @Override
    public MallIssue issueCreate(MallIssue mallIssueBo) {
        //添加了返回主键的insert语句
        mallIssueBo.setDeleted(false);
        mallIssueBo.setAddTime(new Date());
        mallIssueBo.setUpdateTime(new Date());
        int affectedRows = mallIssueMapper.insertSelectiveModify(mallIssueBo);
        if(affectedRows == 0){
            return null;
        }
        MallIssue mallIssueVo = mallIssueMapper.selectByPrimaryKey(mallIssueBo.getId());
        return  mallIssueVo;
    }

    @Override
    public MallIssue issueUpdate(MallIssue mallIssueBo) {
        mallIssueBo.setUpdateTime(new Date());
        int affectedRows = mallIssueMapper.updateByPrimaryKeySelective(mallIssueBo);
        if(affectedRows == 0){
            return null;
        }
        MallIssue mallIssueVo = mallIssueMapper.selectByPrimaryKey(mallIssueBo.getId());
        return  mallIssueVo;
    }

    @Override
    public int issueDelete(MallIssue mallIssue) {

        //逻辑删除，实则更新
        mallIssue.setDeleted(true);
        MallIssueExample example = new MallIssueExample();
        example.createCriteria().andIdEqualTo(mallIssue.getId());
        int affectedRows = mallIssueMapper.updateByExampleSelective(mallIssue, example);
        return affectedRows;
    }
}
